/*
 * Copyright (c) 2008 Kevin Wetzels
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package be.roam.jindy.string;

import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;

/**
 * Utility methods to convert strings to other types.
 * <p>
 * Strings are trimmed: whether you pass in <code>"1"</code> or
 * <code>" 1 "</code>, the result will be the same.
 * </p>
 * 
 * @author Kevin Wetzels
 * 
 */
public class StringConverter {

	/**
	 * Converts the string to an int.
	 * 
	 * @param string
	 *            string to convert
	 * @param defaultValue
	 *            default value to return when the string could not be converted
	 * @return the converted string or the default value if the string could not
	 *         be converted
	 */
	public static int toInt(String string, int defaultValue) {
		if (string == null) {
			return defaultValue;
		}
		try {
			return parseInt(string.trim());
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	/**
	 * Converts the string to an int.
	 * 
	 * @param string
	 *            string to convert
	 * @return the converted string or <code>0</code> when the string could not
	 *         be converted
	 */
	public static int toInt(String string) {
		return toInt(string, 0);
	}

	/**
	 * Converts the string to a long.
	 * 
	 * @param string
	 *            string to convert
	 * @param defaultValue
	 *            default value to return when the string could not be converted
	 * @return the converted string or the default value when the string could
	 *         not be converted
	 */
	public static long toLong(String string, long defaultValue) {
		if (string == null) {
			return defaultValue;
		}
		try {
			return parseLong(string.trim());
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	/**
	 * Converts the string to a long.
	 * 
	 * @param string
	 *            string to convert
	 * @return the converted string or <code>0</code> when the string could not
	 *         be converted
	 */
	public static long toLong(String string) {
		return toLong(string, 0L);
	}

	/**
	 * Converts the string to a boolean.
	 * <p>
	 * Valid string values are <code>true</code>, <code>yes</code> or
	 * <code>1</code>.
	 * </p>
	 * 
	 * @param string
	 *            string to convert
	 * @return <code>true</code> when the string matches (ignoring whitespace)
	 *         <code>true</code>, <code>yes</code> or <code>1</code>
	 */
	public static boolean toBoolean(String string) {
		if (string == null) {
			return false;
		}
		string = string.trim();
		return ("true".equalsIgnoreCase(string) || "yes".equalsIgnoreCase(string) || "1".equalsIgnoreCase(string));
	}

}
